<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>商品列表</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <link rel="stylesheet" href="css/iconfont.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/companydata.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <section class="AllContent" id="AllContent" style="background-color: #fff;">
	    	<div id="productList" style="background-color: #fff;">
	    		<img style="width: 40%;margin:20px auto;display: block;" src="images/loading5.gif">
	    	</div>
	    	
		</section>
        
        <div id="dddd"></div>
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init();
		
		$('.AllHeader li:eq(0)').removeClass('mui-action-back mui-pull-left').addClass('tiaozhuan');
		$('.AllHeader li:eq(1)').html('<input type="text" class="SearchProducts" id="query" onkeypress="if (event.keyCode == 13) { queryProduct(); return false;}" placeholder="输入产品名搜索">')
		$('.AllHeader li:eq(2)').html('<img class="AddProducts" style="width:22px" src="images/addproducts.png">')
		
		//添加商品
		$(".AddProducts").on('tap',function(){
		  mui.openWindow({
		    url: 'AddProducts.html', 
		    id:'AddProducts.html'
		  });
		});
		
		$(function(){
			loadProduct();
			$('.tiaozhuan').on('tap',function(){
				plus.webview.getWebviewById('ProductsList.html').close();
			});
			var RequestTime=1,page_no=1,cishu=1,intZh=1,ptinfo=1;
			$("#AllContent").scroll(function(){
		        var divHeight = $(this).height();
		        var nScrollHeight = $(this)[0].scrollHeight;
		        var nScrollTop = $(this)[0].scrollTop;
		        var xiami = nScrollTop + divHeight;
		        if(nScrollTop + divHeight >= nScrollHeight) {
		            	
		            if(page_no == 1){
		            	var Pages = 2;
		            }else{
		            	Pages = ++page_no
		            }
		            
		            if(RequestTime == cishu){
		        		$('#productList').append("<img id='LoadingImg' style='borderTop:3px solid #f3f3f3;width: 20%;margin:0px auto;display: block;' src='images/loading5.gif'><p style='text-align:center;color:#777;padding:0px 0px;'>正在加载...</p>");
						$(this).animate({scrollTop:xiami},800);
						RequestTime++;
		        		$.ajax({
					        type: "GET",
					        url: getServerUrl() + "/Open/Company/productlist", //与此php页面沟通
					        dataType: 'json', //从php返回的值以 JSON方式 解释
					        data: 'access_token='+getToken()+'&page_no='+Pages, //
					        success: function (json) {//如果调用php成功
					            productlist = json.productlist;
					            UpwardRefresh(productlist);
		       	 				showpage(json.total_results,json.page_no,json.page_size,getwindowhref());
		       	 				page_no = json.page_no;
		       	 				cishu = json.page_no;
		       	 				var ZLength = json.total_results/20;
		       	 				var Strlength = ZLength.toString();
		       	 				if(Strlength.substring(1)){
		       	 					intZh = Strlength.substring(0,1);
		       	 					intZh++
		       	 				}
		       	 				if(intZh == Pages){
					            	RequestTime = 0;
					            }
		       	 				
					        },
					        error:function(json){
					            alertMessage(json.errordescription);
					        }
						});
		        	}else if(RequestTime == 0){
		        		if(ptinfo == 1){
		        			$('#productList').append("<img id='LoadingImg' style='borderTop:3px solid #f3f3f3;width: 20%;margin:0px auto;display: block;' src='images/loading5.gif'><p style='text-align:center;color:#777;padding:0px 0px;'>正在加载...</p>");
							$(this).animate({scrollTop:xiami},800);
		        			
		        			setTimeout(function () {
		        				$('#LoadingImg').attr('src','images/loading5_.jpg');
		        				$('#LoadingImg').next().text('没有更多数据...');
						    }, 1500);
		        			
		        			setTimeout(function () {
							    $('#LoadingImg').next().hide(500);
							    $('#LoadingImg').hide(500);
						    }, 2500);
		        			
		        			ptinfo++;
		        		}
		        	}
		            
		        }
			});
			
		})
		
		function queryProduct() {
            var q = $("#query").val();
            var product = false;
            if (empty(q)) {
                loadProduct();
            } else {
                $.ajax({
                    type: "post",
                    url: getServerUrl() + "/Open/Company/getproduct",
                    dataType: 'json',
                    data: 'access_token=' + getToken()+'&name='+q,
                    success: function (json) {
                        product = json.productlist;
                        showProduct(product);
                        showpage(json.total_results,json.page_no,json.page_size,getwindowhref());
                    },
                    error: function () {
                        alertMessage(L('Please_check_network_connection'));
                    }
                });

            }
            var oTxt=document.getElementById("query"); 
            oTxt.value=""; 
        }
		
		function UpwardRefresh() {
		    var chtml = '';
		    //var chtml = '';
		    if (!empty(productlist)) {
		        for (var i = 0; i < productlist.length; i++) {
		            if (!empty(productlist[i])) {
		            	chtml = chtml + '<div class="ProductsListContent">';
		            	chtml = chtml + '<div class="ProductsDetails">';
		    			chtml = chtml + '<ul>';
		    			chtml = chtml + '<li><img width="100%" src="' + productlist[i].pic_url + '"></li>';
				    	chtml = chtml + '<li>';
				    	chtml = chtml + '<p>' + productlist[i].name + '</p>';
				    	chtml = chtml + '<p>' + productlist[i].price + '</p>';
				    	chtml = chtml + '<p>' + productlist[i].pid + '</p>';
				    	chtml = chtml + '</li>';
		    			chtml = chtml + '</ul>';
		    			chtml = chtml + '</div>';
		    			
		    			chtml = chtml + '<div class="moreOperation">';
		    			chtml = chtml + '<p><a href="EditProduct.html?pid=' + productlist[i].pid + '" >编辑</a></p>';
		    			chtml = chtml + '<p onclick="removeproduct(\'' + productlist[i].pid + '\');">删除</p>';
		    			chtml = chtml + '<p class="prtsBack">取消</p>';
		    			chtml = chtml + '</div>';
		    			chtml = chtml + '</div>';
		            	
		                
		            }
		        }
		    }
		    $('#LoadingImg').next().remove();
		    $('#LoadingImg').remove();
		    $("#productList").append(chtml);
		}
		
		function copyproduct(pid){
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/copyproduct",
                dataType: 'json',
                data: 'access_token=' + getToken()+'&pid='+pid,
                success: function (json) {
                    $("#name").val('复制'+json.name);
                    $("#outer_id").val(json.outer_id);
                    $("#cat_name").val(json.cat_name);
                    $("#price").val(json.price);
                    $("#spec").val(json.spec);
                    $("#description").val(json.description);
                    $("#is_tracking").val(json.is_tracking);
                    $("#unit").val(json.unit);
                    $("#qty").val(json.qty);
                    $("#guarantee_date").val(json.guarantee_date);
                    $("#pic_url").val(json.pic_url);
                    $("#logoimg").attr("src",json.pic_url);
                    $("#note").val(json.note);
					$('.theme-popover-mask').fadeIn(100);
                    $('.theme-popover').slideDown(200);
                    $('body').css({overflow:'hidden'});
                },
                error: function () {
                    alertMessage("请检查网络连接！");
                }
            });
        }
		
		function removeproduct(pid) {
            var r = confirm("请确认是否删除产品?")
            if (r === true)
            {
                $.ajax({
                    type: "post",
                    url: getServerUrl() + "/Open/Company/delproduct",
                    dataType: 'json',
                    data:"access_token="+getToken()+ '&pid=' + pid,
                    success: function (json) {
                        if (json.result == "1") {
                            alertMessage("删除产品完成！");
                            loadProduct();
                        } else {
                            alertMessage("删除产品错误:[" + json.error_description + "]");
                        }
                    }
                });
            }

        }
		
	</script>
</html>
